<?php
/*
此控制器用于控制菜单、上方功能条等布局部分的信息显示，
作为其他控制器的前置方法。在调用此控制器，需注意和
./View/Layout/ 的模板的关联性，不要用display方法直接
输出布局，而是用作其他控制器的布局。

itemnum    学生进行随机练习的答题量
itemid     题目编号
openid     微信用户标识
*/	

namespace Home\Controller;
use Think\Controller;
class LayoutController extends Controller { 
	
	private $itemnum;
	private $_Answer;
	private $wrongnum;
	private $_Collect;
	private $_Testans;
	private $inclassnum;
	private $collectnum;
	private $_Class;
	private $identify;
	private $_Setup;
	
    public function init($openid = "0"){
	
		/*=========定义变量=======*/
		if(is_object($openid)){
            $openid   =   get_object_vars($openid);
        }
		if(empty($openid) || !is_string($openid)) {
            $this->error("openid 错误！");
            return false;
        }
		/*======选择模版======*/
		$_Setup	=	D("setup");
		$theme	=	$_Setup->where("item = 'theme' AND openid='{$openid}'")->getField("value");
		if(!is_null($theme)){
			layout(false);
			layout('Layout/'.$theme);
		}
		
		/*======设定模版变量======*/
		!strlen($openid) || $itemnum = $this->getItemNum($openid);
		$this->getCollectNum($openid);
		$this->getWrongNum($openid);	
		$this->getInClassNum($openid);	
		$this->getIdentify($openid);
		
		$this->assign('itemnum',$itemnum);
		
		$this->assign("wrongnum",$this->wrongnum);
		$this->assign("identify",$this->identify);
		$this->assign("inclassnum",$this->inclassnum);
		$this->assign('collectnum',$this->collectnum);
	
		$this->assign('openid',$openid);							
    }
	public function getItemNum($openid){
		if(isset($_Answer)){
			unset($_Answer);
		}
		$this->_Answer 		= 	D('answer');		
		$this->itemnum  	=   $this->_Answer->where("openid='{$openid}'")->count();
		return $this->itemnum;
	}
	public function getCollectNum($openid){
		$this->_Collect  	= 	D('collect');
		$this->collectnum  	=   $this->_Collect->where("openid='{$openid}'")->count();
		return $this->$collectnum;
	}
	public function getWrongNum($openid){
		$this->_Answer 		= 	D('answer');
		$this->wrongnum 	= 	$this->_Answer->where("openid='{$openid}' AND right_lab = 0")->count();
		return $this->wrongnum;
	}
	public function getInClassNum($openid){
		$this->_Testans 	= 	D('testans');		
		$this->inclassnum  	= 	$this->_Testans->distinct(true)->field('test_num')->where("openid='{$openid}'")->select();				
		$this->inclassnum  	= 	count($this->inclassnum);
		
		return $this->inclassnum;
	}
	public function getIdentify($openid){
		$this->_Class		=	D('class');
		$this->identify 	= 	$this->_Class->where("openid='{$openid}' ")->field("username,number,class")->find();
		
		return $this->identify;
	}
	
}




?>